package com.translate.web.service.impl;

import com.translate.web.config.contract.IOSSConfig;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ExamplePicMapper;
import com.translate.entity.Example;
import com.translate.entity.ExamplePic;
import com.translate.entity.News;
import com.translate.web.service.IExamplePicService;
import com.basic.service.impl.BaseServiceImpl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


@Service
public class ExamplePicServiceImpl extends BaseServiceImpl<ExamplePic> implements IExamplePicService {
	
	private RedisTemplate<String, List<ExamplePic>> redisTemplate;
	
private IOSSConfig config;
	
	@Resource
    public void setConfig(IOSSConfig config) {
		this.config = config;
	}
	
	@Resource
	public void setRedisTemplate(RedisTemplate<String, List<ExamplePic>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public ExamplePicServiceImpl(ExamplePicMapper mapper) {
		super(mapper);
	}
	
	/**
	 * 获取 案例图片列表
	 * @return
	 */
	public List<ExamplePic> examplePics(int[] ids) {
		
		ValueOperations<String, List<ExamplePic>> opsForValue = redisTemplate.opsForValue();
		
		String examplePicKeys = RedisConstant.examplePicKeys(ids);
		
		List<ExamplePic> list = opsForValue.get(examplePicKeys);
		
		if (null != list) {
			return list;
		}
		
		list = this.getDataByIds(ids);
		
		if (0 == list.size()) {
			opsForValue.set(examplePicKeys, list, 3, TimeUnit.SECONDS);
			
			return list;
		}
		
		
		String url = (config. getURL());
		
		for (ExamplePic exPic : list) {
			exPic.setPic(url.concat(exPic.getPic()));
        }
		
		opsForValue.set(examplePicKeys, list, 6, TimeUnit.SECONDS);
		
		return list;
		
	}
	
	

	protected Class<ExamplePic> getEntityClass() {
		return ExamplePic.class;
	}
}